﻿using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Authorization;
using OnWork.Admin.DAL;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace OnWork.Admin.BLL.System
{
    /// <summary>
    /// 授权业务
    /// </summary>
    public class AuthorizationBLL : BaseBLL, OnWork.IScoped
    {
        public AuthorizationBLL(DBManage dBManage) : base(dBManage.System)
        {
        }

        /// <summary>
        /// 账号密码登录
        /// </summary>
        /// <param name="input"></param>
        /// <remarks>用户名/密码：superadmin/123456</remarks>
        /// <returns></returns>
        [AllowAnonymous]
        [DisplayName("账号密码登录")]
        public async Task<LoginOutput> Login([Required] LoginInput input)
        {
            var data = new LoginOutput() { AccessToken = "3333", RefreshToken = "4444" };
            return await Task.FromResult(data);

            ////// 可以根据域名获取具体租户
            ////var host = _httpContextAccessor.HttpContext.Request.Host;

            //// 判断密码错误次数（默认5次，缓存30分钟）
            //var keyErrorPasswordCount = $"{CacheConst.KeyErrorPasswordCount}{input.Account}";
            //var errorPasswordCount = _sysCacheService.Get<int>(keyErrorPasswordCount);
            //if (errorPasswordCount >= 5)
            //    throw Oops.Oh(ErrorCodeEnum.D1027);

            //// 是否开启验证码
            //if (await _sysConfigService.GetConfigValue<bool>(CommonConst.SysCaptcha))
            //{
            //    // 判断验证码
            //    if (!_captcha.Validate(input.CodeId.ToString(), input.Code))
            //        throw Oops.Oh(ErrorCodeEnum.D0008);
            //}

            //// 账号是否存在
            //var user = await _sysUserRep.AsQueryable().Includes(t => t.SysOrg).ClearFilter().FirstAsync(u => u.Account.Equals(input.Account));
            //_ = user ?? throw Oops.Oh(ErrorCodeEnum.D0009);

            //// 账号是否被冻结
            //if (user.Status == StatusEnum.Disable)
            //    throw Oops.Oh(ErrorCodeEnum.D1017);

            //// 租户是否被禁用
            //var tenant = await _sysUserRep.ChangeRepository<SqlSugarRepository<SysTenant>>().GetFirstAsync(u => u.Id == user.TenantId);
            //if (tenant != null && tenant.Status == StatusEnum.Disable)
            //    throw Oops.Oh(ErrorCodeEnum.Z1003);

            //// 国密SM2解密（前端密码传输SM2加密后的）
            //input.Password = CryptogramUtil.SM2Decrypt(input.Password);

            //// 密码是否正确
            //if (CryptogramUtil.CryptoType == CryptogramEnum.MD5.ToString())
            //{
            //    if (!user.Password.Equals(MD5Encryption.Encrypt(input.Password)))
            //    {
            //        _sysCacheService.Set(keyErrorPasswordCount, ++errorPasswordCount, TimeSpan.FromMinutes(30));
            //        throw Oops.Oh(ErrorCodeEnum.D1000);
            //    }
            //}
            //else
            //{
            //    if (!CryptogramUtil.Decrypt(user.Password).Equals(input.Password))
            //    {
            //        _sysCacheService.Set(keyErrorPasswordCount, ++errorPasswordCount, TimeSpan.FromMinutes(30));
            //        throw Oops.Oh(ErrorCodeEnum.D1000);
            //    }
            //}

            //// 登录成功则清空密码错误次数
            //_sysCacheService.Remove(keyErrorPasswordCount);

            //return await CreateToken(user);
        }
    }
}